package org.eduapp.trains.utils;

import javax.servlet.http.*;

import java.net.*;

public class UtilsCookie {
	
	private final static String module = UtilsCookie.class.getName();

 	public static void deleteAllCookie(HttpServletRequest request,
                                     HttpServletResponse response) {
    Cookie rememberMe = UtilsRequest.getCookie(request, "rememberMe");
    if (rememberMe != null)
      UtilsRequest.deleteCookie(response, rememberMe, "/");
    Cookie userCookie = UtilsRequest.getCookie(request, "username");
    if (userCookie != null)
      UtilsRequest.deleteCookie(response, userCookie, "/");
    Cookie passCookie = UtilsRequest.getCookie(request, "password");
    if (passCookie != null)
      UtilsRequest.deleteCookie(response, passCookie, "/");

  }

  public static void setUsername(HttpServletResponse response, String username) {

    UtilsRequest.setCookie(response, "username",
                          UtilsString.encodeString(username),
                          "/");

  }

  public static String getUsername(HttpServletRequest request) {
    String username = "";
    try {
      Cookie userCookie = UtilsRequest.getCookie(request, "username");
      username = (userCookie != null)
          ? UtilsString.decodeString(userCookie.getValue()) : null;
    } catch (Exception e) {
      System.err.print("getUsername from cookie" + e);
    }

    return username;
  }

  public static void setUPassword(HttpServletResponse response, String password) {
    UtilsRequest.setCookie(response, "password",
    		UtilsString.encodeString(password),
                          "/");

  }

  public static String getPassword(HttpServletRequest request) {
    String password = "";
    try {
      Cookie passCookie = UtilsRequest.getCookie(request, "password");
      password = (passCookie != null)
          ? UtilsString.decodeString(passCookie.getValue()) : null;
    } catch (Exception e) {
      System.err.print("getpassCookie from cookie" + e);
    }

    return password;

  }

  public static void setRememberMe(HttpServletResponse response) {
    UtilsRequest.setCookie(response, "rememberMe", "true", "/");
  }

  public static String getRememberMe(HttpServletRequest request) {
    String rememberMe = "";
    try {
      Cookie rememberMeie = UtilsRequest.getCookie(request, "rememberMe");
      rememberMe = (rememberMeie != null)
          ? URLDecoder.decode(rememberMeie.getValue(), "UTF-8") : null;
    } catch (Exception e) {
      System.err.print("getrememberMe from cookie" + e);
    }
    //Debug.logVerbose("[JdonFramework]--> get RememberMe from cookier :" + rememberMe, module);
    return rememberMe;

  }

	public static String getModule() {
		return module;
	}

}
